package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.pojo.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理
 */
@RestController("adminEmployeeController")
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Result<EmployeeLoginVO> login = employeeService.login(employeeLoginDTO);
        return login;
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }
    /**
     * 新增员工
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result insertEmploy(@RequestBody EmployeeDTO employee){
        return employeeService.insertEmploy(employee);
    }

    @GetMapping("page")
    @ApiOperation("分页查询员工")
    public Result<PageResult> selectPageEmploy(EmployeePageQueryDTO dto){
        log.info("前端传过来的参数:{}",dto);
        Result<PageResult> pageResultResult = employeeService.selectPage(dto);
        return  pageResultResult;
    }
    @GetMapping("{id}")
    @ApiOperation("根据id查询员工")
    public Result selectId(@PathVariable Integer id){
        log.info("前端传过来的id:{}",id);
        Employee employee = employeeService.selectId(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result reviseInformation(@RequestBody EmployeeDTO employeeDTO){
        log.info("前端传过来的id:{}",employeeDTO);
        int i = employeeService.reviseInformation(employeeDTO);
        return i==0?Result.success("新增失败"):Result.success("新增成功");
    }
   @PostMapping("status/{status}")
   @ApiOperation("启用、禁用员工账号")
   public Result reviseStatus(@PathVariable(name="status") Integer status, Long id){
       int i = employeeService.reviseStatus(status, id);
       return i==1?Result.success("更新成功"):Result.error("更新失败");
   }

   @PutMapping("editPassword")
   @ApiOperation("修改密码")
   public Result  revisePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("前端传过来修改密码的信息:{}",passwordEditDTO);
       int i = employeeService.revisePassword(passwordEditDTO);
       return i==1?Result.success("更新成功"):Result.error("更新失败");
   }

}
